import request from "@/utils/request"; // axios请求

import type {
  IBaseSaleAttrList,
  ITrademarkList,
  ISpuSaleAttrList,
  IImageList,
  ISpuList,
  ISpu,
  IAttrList,
  IAddSkuParams,
  ISkuList,
} from "./model"; // 数据类型

enum API {
  SPULIST = "/admin/product/", // 获取SPU数据的接口
  BASESALEATTRLIST = "/admin/product/baseSaleAttrList", // 基础销售属性数据的接口
  TRADEMARKLIST = "/admin/product/baseTrademark/getTrademarkList", // 品牌列表数据
  SPUSALEATTRLIST = "/admin/product/spuSaleAttrList/", // spu销售属性数据
  SPUIMAGELIST = "/admin/product/spuImageList/", // spu图片列表数据
  ADDSPU = "/admin/product/saveSpuInfo", // 添加spu
  UPDATESPU = "/admin/product/updateSpuInfo", // 修改spu
  ATTRLIST = "/admin/product/attrInfoList/", // 平台属性
  ADDSKU = "/admin/product/saveSkuInfo", // 添加SKU
  SKUINFO = "/admin/product/findBySpuId/", // sku数据列表
  DELETESPU = "/admin/product/deleteSpu/", // 删除SPU
}

// 获取SPU数据列表
export const reqSpuList = (
  page: number,
  limit: number,
  category3Id: number | string
) =>
  request.get<any, ISpuList>(API.SPULIST + `${page}/${limit}`, {
    params: {
      category3Id,
    },
  });

// 获取基础销售属性数据
export const reqBaseSaleAttrList = () =>
  request.get<any, IBaseSaleAttrList>(API.BASESALEATTRLIST);

// 获取品牌列表数据
export const reqTrademarkList = () =>
  request.get<any, ITrademarkList>(API.TRADEMARKLIST);

// 获取spu销售属性数据
export const reqSpuSaleAttrList = (spuId: number | string | undefined) =>
  request.get<any, ISpuSaleAttrList>(API.SPUSALEATTRLIST + spuId);

// 获取spu图片列表数据
export const reqSpuImageList = (spuId: number | string | undefined) =>
  request.get<any, IImageList>(API.SPUIMAGELIST + spuId);

// 添加或修改spu
export const reqAddOrUpdateSpu = (data: ISpu) => {
  if (data.id) {
    return request.post<any, any>(API.UPDATESPU, data);
  } else {
    return request.post<any, any>(API.ADDSPU, data);
  }
};

// 获取平台属性
export const reqAttrList = (
  category1Id: number | string,
  category2Id: number | string,
  category3Id: number | string
) =>
  request.get<any, IAttrList>(
    API.ATTRLIST + `${category1Id}/${category2Id}/${category3Id}`
  );

// 添加sku
export const reqAddSku = (data: IAddSkuParams) =>
  request.post<any, any>(API.ADDSKU, data);

// 获取sku数据列表
export const reqSkuList = (spuId: number | string | undefined) =>
  request.get<any, ISkuList>(API.SKUINFO + spuId);

// 删除SPU
export const reqDeleteSpu = (spuId: number | string | undefined) =>
  request.delete<any, any>(API.DELETESPU + spuId);
